package com.chookss.home.audit.shop;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.entity.ReimbursementDetailEntity;
import com.chookss.home.entity.ShopEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.RoundCornerImageView;
import com.google.gson.reflect.TypeToken;
import com.lvgroup.hospital.base.BaseAct;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopStatusEditActivity extends BaseAct {
    //物品状态修改
    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvType)
    TextView tvType;
    @BindView(R.id.tvStatus)
    TextView tvStatus;
    @BindView(R.id.ivShop)
    RoundCornerImageView ivShop;
    @BindView(R.id.edContent)
    EditText edContent;
    @BindView(R.id.tvSure)
    TextView tvSure;
    private int type = 2;//2遗失3报废
    private int source = 0;//0我的物品列表 1是详情页
    private ShopEntity shopEntity = null;
    private ReimbursementDetailEntity reimbursementDetailEntity = null;
    private String goodsCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_statu_edit);
        ButterKnife.bind(this);
        initState();
    }

    @Override
    public void initData() {

        type = getIntent().getIntExtra("type", 0);
        source = getIntent().getIntExtra("source", 0);
        goodsCode = getIntent().getStringExtra("goodsCode");
        if (source == 1) {
            tvSure.setText("重新提交");
        }
        selectGoodsInfDetail();

    }

    private void selectGoodsInfDetail() {
        //审批基本数据
        Map<String, String> map = new HashMap<>();
        map.put("projectCode", goodsCode);
        map.put("type", "8");   // 1-新闻 2-视频 3-补卡 4-请假 5-外勤 6-图文7报销8物料9借款
        showLoading();
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<ReimbursementDetailEntity>>() {
        }.getType(), null, Urls.selectKaoqinDeail, SignUtils2.makeSignStr(map), false, new ObserverCallback<ReimbursementDetailEntity>() {
            @Override
            public void onSuccess(ReimbursementDetailEntity data) {
                dismissLoading();
                if (data != null) {
                    if (edContent == null) {
                        return;
                    }
                    reimbursementDetailEntity = data;
                    String title = reimbursementDetailEntity.getGoodsName() + " <font color='#999999'>" + goodsCode + "</font>";
                    tvName.setText(Html.fromHtml(title));
                    tvType.setText("类型:" + reimbursementDetailEntity.getBaoixaoTypeName());
                    if (2 == type) {
                        commonTitleTxt.setText(reimbursementDetailEntity.getOwnerEmployeeName() + "的遗失申请");
                        tvStatus.setText("遗失");
                        tvStatus.setTextColor(Color.parseColor("#F2434D"));
                    } else {
                        commonTitleTxt.setText(reimbursementDetailEntity.getOwnerEmployeeName() + "的报废申请");
                        tvStatus.setText("报废");
                        tvStatus.setTextColor(Color.parseColor("#F2434D"));
//                        tvStatus.setTextColor(Color.parseColor("#9A9A9A"));
                    }
                    if (!Utils.isNull(reimbursementDetailEntity.getImageUrl())) {
                        GlideUtils.load(ShopStatusEditActivity.this, reimbursementDetailEntity.getImageUrl(), ivShop, R.drawable.default_news);
                    } else {
                        boolean haveUrl = false;
                        for (int i = 0; i < reimbursementDetailEntity.getAnnexs().size(); i++) {
                            for (int j = 0; j < reimbursementDetailEntity.getAnnexs().get(i).getPictureInfoList().size(); j++) {
                                if (j == 0 && !haveUrl) {
                                    haveUrl = true;
                                    reimbursementDetailEntity.setImageUrl(reimbursementDetailEntity.getAnnexs().get(i).getPictureInfoList().get(j).getPictureUrl());
                                    GlideUtils.load(ShopStatusEditActivity.this, reimbursementDetailEntity.getAnnexs().get(i).getPictureInfoList().get(j).getPictureUrl(), ivShop, R.drawable.default_news);
                                }
                            }
                        }
                        if (!haveUrl) {
                            GlideUtils.load(ShopStatusEditActivity.this, "", ivShop, R.drawable.default_news);
                        }
                    }
                }
            }

            @Override
            public void onError(String errorMsg) {
                dismissLoading();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    private void updateGoodsStatus() {
        Map<String, String> map = new HashMap<>();
        map.put("orgCode", reimbursementDetailEntity.getOrgCode());
        map.put("amt", reimbursementDetailEntity.getAmt());
        map.put("workflowId", reimbursementDetailEntity.getWorkflowId());
        map.put("goodsCode", goodsCode);
        map.put("goodsName", reimbursementDetailEntity.getGoodsName());
        map.put("goodsNum","1");
        map.put("groupCode", reimbursementDetailEntity.getGroupCode());
        map.put("groupName", reimbursementDetailEntity.getGroupName());
        map.put("baoixaoTypeCode", reimbursementDetailEntity.getBaoixaoTypeCode());
        map.put("baoxiaoTypeName", reimbursementDetailEntity.getBaoixaoTypeName());
        map.put("statusCd", type + "");
        map.put("ycRemark", edContent.getText().toString());
        showLoading();
        MyHttpRequest.postRequest(Urls.updateGoodsStatus, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    dismissLoading();
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        MyToast.show("提交成功");
                        EventBus.getDefault().post(new MyEvent("RefreshShopRecord"));
                        finish();
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    dismissLoading();
                    MyToast.show("提交失败，请稍后重试");
                }
            }
        });
    }

    @OnClick({R.id.common_title_back, R.id.tvSure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.tvSure:
                if (Urls.antiShake.check()) {
                    return;
                }
                if (Utils.isNull(edContent.getText().toString())) {
                    MyToast.show("请输入说明");
                    return;
                }
                updateGoodsStatus();
                break;
        }
    }

}
